package com.hzjy.api.service.impl;

import com.hzjy.api.beans.HzjyAgreement;
import com.hzjy.api.beans.HzjyDelist;
import com.hzjy.api.mapper.ApiDelistMapper;
import com.hzjy.api.mapper.ApiProductMapper;
import com.hzjy.common.utils.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 摘牌
 */
@Service
public class ApiDelistServiceImpl {
    private static final Logger log = LoggerFactory.getLogger(ApiProductServiceImpl.class);
    @Autowired
    private ApiDelistMapper apiDelistMapper;
    @Autowired
    private ApiProductMapper apiProductMapper;
    @Autowired
    private HzjyAgreementServiceImpl hzjyAgreementService;

    /**
     * 查询摘牌申请
     *
     * @param delistId 摘牌申请ID
     * @return 摘牌申请
     */
    public HzjyDelist selectHzjyDelistById(Long delistId) {
        return apiDelistMapper.selectHzjyDelistById(delistId);
    }

    /**
     * 查询摘牌申请列表
     *
     * @param hzjyDelist 摘牌申请
     * @return 摘牌申请
     */
    public List<HzjyDelist> selectHzjyDelistList(HzjyDelist hzjyDelist) {
        return apiDelistMapper.selectHzjyDelistList(hzjyDelist);
    }

    /**
     * 新增摘牌申请
     *
     * @param hzjyDelist 摘牌申请
     * @return 结果
     */
    public int insertHzjyDelist(HzjyDelist hzjyDelist) {
        return apiDelistMapper.insertHzjyDelist(hzjyDelist);
    }

    /**
     * 修改摘牌申请
     *
     * @param hzjyDelist 摘牌申请
     * @return 结果
     */
    public int updateHzjyDelist(HzjyDelist hzjyDelist) {
        return apiDelistMapper.updateHzjyDelist(hzjyDelist);
    }

    /**
     * 批量删除摘牌申请
     *
     * @param delistIds 需要删除的摘牌申请ID
     * @return 结果
     */
    public int deleteHzjyDelistByIds(Long[] delistIds) {
        return apiDelistMapper.deleteHzjyDelistByIds(delistIds);
    }

    /**
     * 删除摘牌申请信息
     *
     * @param delistId 摘牌申请ID
     * @return 结果
     */
    public int deleteHzjyDelistById(Long delistId) {
        return apiDelistMapper.deleteHzjyDelistById(delistId);
    }

    /**
     * 查询摘牌申请列表（分页） currentPage第几页,size页数,status状态
     *
     * @param pd
     * @return 摘牌申请对象（包含作品对象）
     */
    public Map selectHzjyDelistList(PageData pd) {
        Map map = new HashMap();
        List<HzjyDelist> list = apiDelistMapper.selectHzjyDelistPageList(pd);
        Integer count = apiDelistMapper.selectHzjyDelistPageCount(pd);
        map.put("totalResult", count);
        if (!CollectionUtils.isEmpty(list)) {
            for (HzjyDelist hzjyDelist : list) {
                PageData productData = apiProductMapper.selectHzjyProductByKeyId(hzjyDelist.getProductId());
                //查询合同信息计算授权结束时间
                HzjyAgreement hzjyAgreement = new HzjyAgreement();
                hzjyAgreement.setDelistId(hzjyDelist.getDelistId());
                List<HzjyAgreement> hzjyAgreementList = hzjyAgreementService.selectHzjyAgreementList(hzjyAgreement);
                if (!CollectionUtils.isEmpty(hzjyAgreementList)) {
                    hzjyDelist.setDelistEndTime(hzjyAgreementList.get(0).getFinishTimeDate());
                    hzjyDelist.setContractId(hzjyAgreementList.get(0).getId().toString());
                }
                hzjyDelist.setProductObj(productData);
            }
        }
        map.put("delistList", list);

        return map;
    }

    /**
     * 获取我的摘牌各状态的数量
     */
    public Map getDelistingStateNum(Long userId) {
        List<PageData> pd = apiDelistMapper.getDelistingStateNum(userId);
        Map map = new HashMap();
        for (PageData p : pd) {
            switch (p.get("type").toString()) {
                case "whole":
                    map.put("whole", Integer.parseInt(p.get("num").toString()));
                    break;
                case "wait"://待审核
                    map.put("wait", Integer.parseInt(p.get("num").toString()));
                    break;
                case "unSigned"://待签订
                    map.put("unSigned", Integer.parseInt(p.get("num").toString()));
                    break;
                case "delivery"://交货中
                    map.put("delivery", Integer.parseInt(p.get("num").toString()));
                    break;
                case "delistSuccess"://摘牌成功
                    map.put("delistSuccess", Integer.parseInt(p.get("num").toString()));
                    break;
            }
        }
        return map;
    }

    /**
     * 查询签约超时的摘牌
     *
     * @return
     */
    public List<HzjyDelist> querySignTimeOutDelist() {
        return apiDelistMapper.querySignTimeOutDelist();
    }

}
